@include('admin._include.header')
<body>


<div class="layui-fluid">
    <div class="layui-row larryms-panel auth-user-add">
        <form class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">权限组名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required" placeholder="请填写权限组名称"  value="{{$group->name}}" autocomplete="off" class="layui-input larry-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">权限设置</label>
                <div class="layui-input-block">
                    <div id="menuTree" class="demo-tree"></div>
                </div>
            </div>


            <div class="layui-form-item" style="text-align: center">
                <input type="hidden" name="id" value="{{$group->id}}">
                <button class="layui-btn" lay-submit lay-filter="add_group">确定</button>
            </div>


        </form>
    </div>
</div>

</body>
<script>

    $(function () {
        layui.use(['tree', 'util', 'form'], function () {
            var form = layui.form;
            var tree = layui.tree;

            var index = layer.load(1);
            $.ajax({
                url: "{{url('auth/group-tree')}}",
                dateType: 'json',
                data: {'id': "{{$group->id}}"},
                type: 'post',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function (res) {
                    layer.close(index);
                    if (res.status == SUCCESS) {
                        tree.render({
                            elem: '#menuTree',
                            id:'menuTree',
                            data: res.data.menu,
                            showCheckbox: true,
                        });

                    }
                }
            });



            form.on('submit(add_group)', function(data) {

                var menu_id = tree.getChecked('menuTree');
                var index = layer.load(1);

                $.ajax({
                    url:"{{url('auth/edit-group')}}",
                    dateType:'json',
                    data:{
                        id: data.field.id,
                        name: data.field.name,
                        menu_id: menu_id
                    },
                    type:'post',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    success:function (res) {
                        layer.close(index);
                        layer.msg(res.message);
                        if(res.status == SUCCESS){
                            var iframe = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(iframe); //再执行关闭
                        }

                    }
                });

                return false;
            });

        });

    });






</script>
@include('admin._include.footer')